package com.inspectortime.domain.validate;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.inspectortime.domain.InspectorCompany;
import com.inspectortime.repository.Repository;
import com.mkode.util.java.lang.ClassUtils;

public class InspectorCompanyValidator implements Validator {

	private int minAllowedServiceMiles;
	private int maxAllowedServiceMiles;
	
	private Repository repository;
	public void setRepository(Repository repository) {
		this.repository = repository;
	}

	public boolean supports(Class clazz) {
		return ClassUtils.isInstanceOf(clazz,  InspectorCompany.class);
	}

	public void validateForDuplicates(Object target, Errors errors) {
		InspectorCompany company = (InspectorCompany) target;
		InspectorCompany duplicateCompany = null;
		if (!StringUtils.isBlank(company.getName())) {
			duplicateCompany = repository.findInspectorCompanyByName(company.getName());
			if (duplicateCompany != null) {
				errors.rejectValue("name", "duplicate", null, "Company name not available");
			}
		}
		if (!StringUtils.isBlank(company.getShortName())) {
			duplicateCompany = repository.findInspectorCompanyByShortName(company.getShortName());
			if (duplicateCompany != null) {
				errors.rejectValue("shortName", "duplicate", null, "Company shortname not available");
			}
		}
	}
	
	public void validate(Object target, Errors errors) {
		InspectorCompany company = (InspectorCompany) target;

		// Required
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "shortName", "required");	
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "required");	
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address.street1", "required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address.city", "required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address.state", "required");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address.zipCode", "required");
		
		validateMilesServed(target, errors);
		
		// Format
		if (!StringUtils.isBlank(company.getShortName())) {
			if (!com.inspectortime.util.ValidationUtils.isValidCompanyShortName(company.getShortName())) {
				errors.rejectValue("shortName", "invalid.company.shortName");
			}
		}
	}

	public void validateMilesServed(Object target, Errors errors) {
		InspectorCompany company = (InspectorCompany) target;
		if (company.getMaxMilesServed() < minAllowedServiceMiles || company.getMaxMilesServed() > maxAllowedServiceMiles){
			String[] errorArgs = {String.valueOf(minAllowedServiceMiles), String.valueOf(maxAllowedServiceMiles)};
			errors.rejectValue("maxMilesServed", "invalid.company.miles.served", errorArgs, "Invalid number of miles.");	
		}
	}

	public void setMinAllowedServiceMiles(int minAllowedServiceMiles) {
		this.minAllowedServiceMiles = minAllowedServiceMiles;
	}

	public void setMaxAllowedServiceMiles(int maxAllowedServiceMiles) {
		this.maxAllowedServiceMiles = maxAllowedServiceMiles;
	}

	
	
}
